﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.UnifyShow.Business;
using Microsoft.Practices.Unity;
using Wicresoft.UnifyShow.Portal.Models;

namespace Wicresoft.UnifyShow.Portal.Controllers
{
    public class NavigateController : BaseController
    {
        private IUser_X user_x;
        private ISiteBranch siteBranch;

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)user_x).Dispose();
            ((IDisposable)siteBranch).Dispose();
        }

        [InjectionConstructor]
        public NavigateController(IUser_X iuser, ISiteBranch siteBranch)
        {
            this.user_x = iuser;
            this.siteBranch = siteBranch;
        }

        public ActionResult Report()
        {
            return View();
        }

        public ActionResult Index()
        {
            //获取用户信息（权限）
            base.SetRoles(user_x);
            int userID = (int)UserID;
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            User_XModel usermodel = user_x.GetUserByID((decimal)userID);
            if (usermodel == null) return new HttpUnauthorizedResult();

            NavigateModel navigateModel = new NavigateModel
            {
                AccessITSMName = usermodel.AccessITSM == 1 ? NavigateSystemName.ITSM : "",
                AccessUSName = usermodel.AccessUS == 1 ? NavigateSystemName.US : "",
                AccessGJName = usermodel.AccessGJ == 1 ? NavigateSystemName.GJ : "",
                IsLeader = usermodel.IsLeader == 1,
                SiteBranch_GJ = usermodel.AccessGJ == 1 ? siteBranch.GetSiteBranch_GJByUserID(userID) : (new List<SiteBranchModel>())
            };
            return View(navigateModel);
        }

        //private IUser_X user_x;
        //private ISiteBranch siteBranch;
        //private IIncident incident;
        //private IProblem problem;
        //private IChange change;
        //private IAlert alert;
        //private ISiteBranchAndDeviceAndAlert siteBranchAndDeviceAndAlert;

        //protected override void Dispose(bool disposing)
        //{
        //    ((IDisposable)user_x).Dispose();
        //    ((IDisposable)siteBranch).Dispose();
        //    ((IDisposable)incident).Dispose();
        //    ((IDisposable)problem).Dispose();
        //    ((IDisposable)change).Dispose();
        //    ((IDisposable)alert).Dispose();
        //    ((IDisposable)siteBranchAndDeviceAndAlert).Dispose();
        //}

        //[InjectionConstructor]
        //public NavigateController(IUser_X iuser, ISiteBranch isiteBranch, IIncident iincident, IProblem iproblem, IChange ichange, IAlert ialert, ISiteBranchAndDeviceAndAlert siteBranchAndDeviceAndAlert)
        //{
        //    this.user_x = iuser;
        //    this.siteBranch = isiteBranch;
        //    this.incident = iincident;
        //    this.problem = iproblem;
        //    this.change = ichange;
        //    this.alert = ialert;
        //    this.siteBranchAndDeviceAndAlert = siteBranchAndDeviceAndAlert;
        //}


        //public ActionResult Index()
        //{
        //    //获取用户信息（权限）
        //    base.SetRoles(user_x);
        //    int userID = (int)UserID;
        //    ViewBag.IsConfigAdmin = IsConfigAdmin;
        //    User_XModel usermodel = user_x.GetUserByID((decimal)userID);
        //    if (usermodel == null) return new HttpUnauthorizedResult();

        //    NavigateModel navigateModel = new NavigateModel
        //    {
        //        AccessITSMName = usermodel.AccessITSM == 1 ? NavigateSystemName.ITSM : "",
        //        AccessUSName = usermodel.AccessUS == 1 ? NavigateSystemName.US : "",
        //        AccessGJName = usermodel.AccessGJ == 1 ? NavigateSystemName.GJ : "",
        //        IsLeader = usermodel.IsLeader == 1,
        //        SiteBranch_GJ = usermodel.AccessGJ == 1 ? siteBranch.GetSiteBranch_GJByUserID(userID) : (new List<SiteBranchModel>())
        //    };
        //    List<SiteBranchModel> sites = siteBranch.GetSiteALLBranch();
        //    //用户拥有ITSM系统权限，查看所在地市的未解决事件
        //    List<IncidentModel> incidentlist = new List<IncidentModel>();
        //    if (usermodel.AccessITSM == 1)
        //    {
        //        incidentlist = incident.GetUnResolvedIncidentList((int)(sites.FirstOrDefault(o => string.Equals(o.Name, usermodel.Company)).ID), null, null, 5, 0);
        //    }
        //    ViewData["incident"] = incidentlist;

        //    //用户拥有告警系统权限，查看所在地市的未关闭告警
        //    List<AlertModel> alertlist = new List<AlertModel>();
        //    if (usermodel.AccessITSM == 1)
        //    {
        //        alertlist = alert.GetOpenAlertBySiteID((int)(sites.FirstOrDefault(o => string.Equals(o.Name, usermodel.Company)).ID),null, null, 5, 0);
        //    }
        //    ViewData["alert"] = alertlist;

        //    //领导权限：查看ITSM工单统计
        //    List<SiteBranchModel> siteBranchList = siteBranch.GetSiteALLBranch();
        //    NavigateITSMModel navigateITSMList = new NavigateITSMModel();
        //    if (usermodel.IsLeader == 1)
        //    {
        //        List<NavigateSiteITSMModel> ITSMModelList = new List<NavigateSiteITSMModel>();
        //        int TotalIncidentCount = 0;
        //        int TotalProblemCount = 0;
        //        int TotalChangeCount = 0;
        //        foreach (var item in siteBranchList)
        //        {
        //            NavigateSiteITSMModel model = new NavigateSiteITSMModel();
        //            model.SiteName = item.Alias;
        //            model.IncidentCount = incident.GetUnResolvedIncidentList((int)item.ID, null, null, null, null).Count;
        //            TotalIncidentCount += model.IncidentCount;
        //            model.ProblemCount = problem.GetUnResolvedProblemCount((int)item.ID, null, null);
        //            TotalProblemCount += model.ProblemCount;
        //            model.ChangeCount = change.GetUnResolvedChangeCount((int)item.ID, null, null);
        //            TotalChangeCount += model.ChangeCount;
        //            ITSMModelList.Add(model);
        //        };
        //        navigateITSMList.ITSMModelList = ITSMModelList;
        //        navigateITSMList.TotalIncidentCount = TotalIncidentCount;
        //        navigateITSMList.TotalProblemCount = TotalProblemCount;
        //        navigateITSMList.TotalChangeCount = TotalChangeCount;
        //    }
        //    ViewData["navigateITSMList"] = navigateITSMList;

        //    //领导权限：查看告警统计
        //    NavigateAlertModel navigateAlertModel = new NavigateAlertModel();
        //    if (usermodel.IsLeader == 1)
        //    {
        //        List<NavigateSiteAlertModel> siteAlertList = new List<NavigateSiteAlertModel>();
        //        int alertCount = 0;
        //        foreach (var item in siteBranchList)
        //        {
        //            SiteBranchAndDeviceTypeAlertModel model = siteBranchAndDeviceAndAlert.getSiteBranchAndDeviceAndAlert((int)item.ID);
        //            NavigateSiteAlertModel navigateSiteAlertModel = new NavigateSiteAlertModel();
        //            navigateSiteAlertModel.SiteBranchName = item.Alias;
        //            navigateSiteAlertModel.DeviceTypeAlert = model.DeviceTypeAlert;
        //            int siteAlertCount = 0;
        //            foreach (var dta in model.DeviceTypeAlert)
        //            {
        //                siteAlertCount += (int)dta.AlertCount;
        //            }
        //            navigateSiteAlertModel.SiteAlertCount = siteAlertCount;
        //            siteAlertList.Add(navigateSiteAlertModel);
        //            alertCount += siteAlertCount;
        //        };
        //        navigateAlertModel.AlertCount = alertCount;
        //        navigateAlertModel.SiteAlertList = siteAlertList;
        //    }
        //    ViewData["navigateAlertModel"] = navigateAlertModel;



        //    return View(navigateModel);
        //}

    }
}
